package net.boot.smpp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author qiusu
 * @since  2025/08/18
 */
@Configuration
@EnableAsync // 启用异步支持
public class ThreadPoolConfig {
    
    @Bean(name = "waitBindExecService")
    public ThreadPoolTaskExecutor waitBindExecService() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(3);
        // 最大线程数
        executor.setMaxPoolSize(10);
        // 队列容量
        executor.setQueueCapacity(100);
        // 设置等待任务完成时的关闭行为
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(5);
        // 线程名前缀
        executor.setThreadNamePrefix("pool-waitBind-");
        // 拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean(name = "deliveryExecService")
    public ThreadPoolTaskScheduler deliveryExecService() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // 核心线程数
        scheduler.setPoolSize(5);
        // 设置等待任务完成时的关闭行为
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        scheduler.setAwaitTerminationSeconds(5);
        // 线程名前缀
        scheduler.setThreadNamePrefix("scheduler-delivery-");
        // 拒绝策略
        scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        scheduler.initialize();
        return scheduler;
    }

}